<template>
  <div>
    <el-dialog
      title="用户选择"
      :visible.sync="dialogVisible"
      width="80%"
      :before-close="changedialogVisibleDis"
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="98px"
        size="mini"
      >
        <el-form-item label="员工编号" prop="userNo">
          <el-input
            v-model="queryParams.userNo"
            placeholder="请输入员工编号"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="姓名" prop="nickName">
          <el-input
            v-model="queryParams.nickName"
            placeholder="请输入姓名"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="主管单位" prop="competentOrg">
          <el-input
            v-model="queryParams.competentOrg"
            placeholder="请输入主管单位"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="主管部门" prop="deptName">
          <el-input
            v-model="queryParams.deptName"
            placeholder="请输入主管部门"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="在职状态" prop="incumbencyContent">
          <el-select
            v-model="queryParams.incumbencyContent"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in jobStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="人员类别" prop="personTypeContent">
          <el-select
            v-model="queryParams.personTypeContent"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in peopleTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            type="cyan"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-divider></el-divider>
      <el-form
        :inline="true"
        v-show="showSearch"
        label-width="98px"
        size="mini"
      >
     
       <el-form-item label="选择的人员" prop="ids">
          <el-select
            v-model="ids"
            multiple
            clearable
            size="middle"
            style="width: 1000px"
          >
          <el-option
                  v-for="item in  checkedOptions"
                  :key="item.id"
                  :label="item.value"
                  :value="item.id"
                ></el-option>
          </el-select>
        </el-form-item>
      </el-form>


      <el-table
        ref="materialTable"
        v-loading="loading"
        row-key="id"
        :data="userList"
        :default-sort="{ prop: 'userNo', order: 'ascending' }"
        @sort-change="tableSortChange"
        @selection-change="handleSelectionChange"
        :cell-style="{
          'font-size': '11px',
          'padding-left': '0px',
          'padding-right': '0px',
        }"
        size="mini"
      >
        <el-table-column
          type="selection"
          :reserve-selection="true"
          width="55"
          align="center"
        />
        <el-table-column
          label="员工编号"
          align="center"
          prop="userNo"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        />
        <el-table-column
          label="姓名"
          align="center"
          prop="nickName"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        />

        <el-table-column
          label="主管单位"
          align="center"
          prop="competentOrg"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        />
        <el-table-column label="主管部门" align="center" prop="dept.deptName" />
        <el-table-column
          label="在职状态"
          align="center"
          prop="incumbencyContent"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        />
        <el-table-column
          label="人员类别"
          align="center"
          prop="personTypeContent"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        />
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <span slot="footer" class="dialog-footer">
        <el-button @click="changedialogVisibleDis">取 消</el-button>
        <el-button type="primary" @click="changedialogVisible">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  listUserNew,
  checkedListSearch,
  addUser,
  updateUser,
  changeUserStatus,
  importTemplate,
  getUserInfoByJobNoOrPhone,
} from "@/api/system/user";
import elementIcons from '../../views/components/icons/element-icons';
export default {
  name: "ChooseUser",
  components: {},
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    userId: {
      type: String,
      default: "",
    },
    userName: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      userDatas: "测试",
      // 遮罩层
      loading: true,
      jobStatusOptions: [],
      peopleTypeOptions: [],
      //被选中的人员数据 
      checkedOptions: [],
      // 选中数组
      ids: [],
      names: [],
      selectFlag:false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 部门同步中间表格数据
      userList: [],
      newIds: [],
      newNames: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined,
        deptName: undefined,
        incumbencyContent: undefined,
        personTypeContent: undefined,
        projectLeader: undefined,
        portalRole: undefined,
        userNo: undefined,
        competentOrg: undefined,
      },
    };
  },

  mounted() {},
  created() {
    this.getDicts("portal_people_type").then((response) => {
      this.peopleTypeOptions = response.data;
    });
    this.getDicts("portal_job_status").then((response) => {
      this.jobStatusOptions = response.data;
    });
    this.getList();
  },
  watch: {
    userId: function (newVal, oldVal) {
      if (!newVal) {
        this.isClean;
        this.newIds=[];
        this.ids=[];
      } else {
        this.newIds = newVal.split(";");
        this.ids =  this.newIds;
        this.checkedListSearch();
      }
    },
    ids:function(newVal, oldVal){
      console.log("ids变化了新值：",newVal," 旧值：",oldVal)
       this.checkedListSearch();
    }
  },
  methods: {
    checkRow() {
      this.$refs.materialTable.clearSelection();
      var $this = this;
      $this.$nextTick(() => {
        this.newIds.forEach((item,index) => {
          var row = {
            id: item,
            nickName: $this.newNames[index]
          };
          $this.$refs.materialTable.toggleRowSelection(row, true);
        });
      });
      this.resetQuery();
    },
    isClean() {
      this.$refs.materialTable.clearSelection();
    },
    changedialogVisibleDis() {
      this.$emit("changetabdialogVisible", false);
      this.ids = [];
      this.names = [];
    },
    changedialogVisible() {
      let option = this.checkedOptions;
      console.log("option ->", option);
      debugger;
       let newNamed = "";
       if(option && option!=null){
        for (let index = 0; index < option.length; index++) {
          const element = option[index];
          newNamed = newNamed + element.value + ";"
        }
       this.names = newNamed;
       this.newNames = newNamed;
       this.$emit("chooseUserNames", this.names);
      }
      
        
     
      this.$emit("changetabdialogVisible", false);
      this.$emit("chooseUserIds", this.ids);
    },
    getList() {
      this.loading = true;
      listUserNew(this.queryParams).then((response) => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectFlag = true;
      this.ids = selection.map((item) => item.id);
      // this.names = selection.map((item) => item.nickName+"("+item.userNo+")");
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    tableSortChange(column) {
      this.queryParams.pageNum = 1;
      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getList();
    },
    //选中的数据
    checkedListSearch(){
      let userIds = this.ids;
      if( userIds != null && userIds.length > 0){
        //文本类型和数字类型不兼容，转换
        for (let index = 0; index < userIds.length; index++) {
          const element = userIds[index];
          let intStr = parseInt(element);
          this.ids[index]=intStr;
        }
        checkedListSearch(userIds).then((response) => {
          this.checkedOptions = response;
        });
      }else{
         this.checkedOptions = [];
      }
    },
  },
};
</script>
